<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改图书')" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form enctype="multipart/form-data" class="form-horizontal m" id="form-book-edit" th:object="${libBook}">
            <input name="bookId" th:field="*{bookId}" type="hidden">
            <input id="typeVal" th:value="*{typeId}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">图书编号：</label>
                <div class="col-sm-8">
                    <input name="bookNo" th:field="*{bookNo}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">图书名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">图书介绍：</label>
                <div class="col-sm-8">
                    <textarea name="introduce" maxlength="100"  th:field="*{introduce}" class="form-control" type="text" placeholder="请输入图书简介"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图书图片：</label>
                <div class="col-sm-8">
                    <div id="screen" class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;">
                            <img th:src="*{images}">
                        </div>
                        <div>
                    <span class="btn btn-white btn-file">
                        <span class="fileinput-new">选择图片</span><span class="fileinput-exists">更改</span>
                        <input name="picture" type="file" id="inputImage" accept="image/*"></span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">价格：</label>
                <div class="col-sm-8">
                    <input name="price" th:field="*{price}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select class="form-control m-b" name="typeId" id="selectType"></select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script th:inline="javascript">
        //更新专属的图片预览逻辑
        $(function() {
            var ment = $('.fileinput-exists');
            ment.show();
            ment.eq(0).prev().hide();
            $('#screen').on('clear.bs.fileinput', function (e) {
                ment.hide();
                ment.eq(0).prev().show();
            });
            $('#screen').on('change.bs.fileinput', function (e) {
                ment.show();
                ment.eq(0).prev().hide();
                var file = $('#inputImage')[0].files[0];
                if (!/^image\/\w+$/.test(file.type)) {
                    $.modal.alertWarning('请选择一个图片文件。');
                    $('#screen').fileinput('clear');
                    ment.hide();
                    ment.eq(0).prev().show();
                }
            });

            //类型下拉框初始化
            let view = $('#selectType');
            let view2 = '';
            let typeId = $('#typeVal').val();
            $.ajax({
                url:ctx + 'admin/type/listNotPage',
                type:'post',
                dataType:'json',
                success: function(result) {
                    result.rows.map( item => {
                        if(typeId == item.typeId) {
                            view.append('<option value="'+ item.typeId +'">'+ item.name +'</option>');
                        } else {
                            view2 += '<option value="'+ item.typeId +'">'+ item.name +'</option>';
                        }
                    });
                    view.append(view2);
                }
            });
        });

        var prefix = ctx + "admin/book";

        $("#form-book-edit").validate({
            onkeyup: false,
            rules:{
                price:{
                    number:true
                },
                bookNo: {
                    isUserName: true,
                    remote: {
                        url: prefix + "/checkBookNo",
                        type: "post",
                        dataType: "json",
                        data: {
                            "bookId": function() {
                                return $("#bookId").val();
                            },
                            "bookNo": function () {
                                return $.common.trim($("#bookNo").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "bookNo": {
                    remote: "图书编号已经存在"
                },
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var formdata = new FormData($('#form-book-edit')[0]);
                $.ajax({
                    url:prefix + '/edit',
                    type:'post',
                    data:formdata,
                    processData: false,
                    contentType: false,
                    dataType:'json',
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                        $.modal.disable();
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.successCallback(result);
                    }
                });
            }
        }
    </script>
</body>
</html>